package com.heer.gymnasium.adapter;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.heer.api.ResponseData.ApiResponse;
import com.heer.api.callback.BaseCallback;
import com.heer.gymnasium.AppHelper;
import com.heer.gymnasium.R;
import com.heer.gymnasium.util.LoadImageUtil;
import com.heer.model.ChuRuOrderBean;
import com.heer.model.QrcodeAccess;
import com.zbar.lib.utils.QRImageUtils;

import java.util.Timer;
import java.util.TimerTask;

import butterknife.BindView;
import butterknife.ButterKnife;
import retrofit2.Call;
import retrofit2.Response;

/**
 * Created by admin on 2017/7/12.
 */

public class ChuruListAdapter extends ListAdapter<ChuRuOrderBean> {

    protected AppHelper mAppHelper;
    public static TimerTask task;
    public static  Timer timer;

    Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            loadQRImage(msg.getData().getString("oid"),(ImageView) msg.obj);
        }
    };
    public ChuruListAdapter(Context context) {
        super(context);
        mAppHelper = AppHelper.context();
    }

    @Override
    public View getView(int position, View view, ViewGroup parent) {
        final ViewHolder holder;
        if (view==null)
        {

            view=mInflater.inflate(R.layout.item_churuchang_list, parent, false);
            holder=new ViewHolder(view);
            view.setTag(holder);

        }else {
            holder= (ViewHolder) view.getTag();
        }

        final ChuRuOrderBean item=getItem(position);

        holder.tv_name.setText(item.getTicket_name());
        holder.tv_time.setText(item.getStime()+"~"+item.getEtime());
        holder.tv_title.setText(item.getStadiumname());
        Glide.with(mContext)
                .load(LoadImageUtil.getLoadImageUrl(item.getStadiumpic()))
                .placeholder(R.mipmap.ic_order_card)
                .into(holder.iv_scene);
        if (item.isSqshow())
        {
            holder.iv_sq.setVisibility(View.VISIBLE);
        }else {
            holder.iv_sq.setVisibility(View.GONE);
        }
        holder.tv_ruchang.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                for (int i=0;i<getCount();i++)
                {
                    getItem(i).setSqshow(false);
                }
                item.setSqshow(true);
                loadQRImage(item.getOrder_sn(),holder.iv_sq);
                notifyDataSetChanged();
                if (task!=null)
                {
                    task.cancel();
                }
                if (timer!=null){
                    timer.cancel();
                }
                task=new TimerTask() {
                       @Override
                       public void run() {
                           Message msg=new Message();
                           msg.obj=holder.iv_sq;
                           Bundle bundle=new Bundle();
                           bundle.putString("oid",item.getOrder_sn());
                           msg.setData(bundle);
                           handler.sendMessage(msg);
                       }
                };

                    timer=new Timer();
                    timer.schedule(task,60*60*1000,60*60*1000);


            }
        });




        return view;


    }
    private void loadQRImage(String oid, final ImageView iv) {
        mAppHelper.apiService().getAccessQrcode(oid).enqueue(new BaseCallback<ApiResponse<QrcodeAccess>>() {
            @Override
            public void onResponse(Call<ApiResponse<QrcodeAccess>> call, Response<ApiResponse<QrcodeAccess>> response) {
                super.onResponse(call, response);
                if (response.isSuccessful() && response.body().isSccessful()) {
                    iv.setImageBitmap(new QRImageUtils(mContext).createQRImage(response.body().getData().getQRstr()));
                }
            }

            @Override
            public void onFailure(Call<ApiResponse<QrcodeAccess>> call, Throwable t) {
                super.onFailure(call, t);
            }
        });
    }

    static class ViewHolder {
        @BindView(R.id.tv_churu_title)
        TextView tv_title;

        @BindView(R.id.tv_churu_name)
        TextView tv_name;

        @BindView(R.id.tv_churu_time)
        TextView tv_time;

        @BindView(R.id.iv_churu_sq)
        ImageView iv_sq;

        @BindView(R.id.image_churu_scene)
        ImageView iv_scene;

        @BindView(R.id.btn_churu_2)
        TextView tv_ruchang;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
